package citylist

import (
	"go-reptile/crawler/engine"
	"regexp"
	"strconv"
	 "go-reptile/crawler/model"
)
var guessRe = regexp.MustCompile(`href="http://album.zhenai.com/u/([\d]+)">([^<]+)</a>`)
var ageRe =regexp.MustCompile( `<td><span class="label">年龄：</span>([\d]+)岁</td>`)
var marriageRe = regexp.MustCompile(`<td><span class="label">婚况：</span>([^<]+)</td>`)
var heightRe = regexp.MustCompile(`<td><span class="label">身高：</span>([^<]+)</td>`)
var incomeRe = regexp.MustCompile(`<td><span class="label">月收入：</span>([\d]+)+CM</td>`)
var weightRe = regexp.MustCompile(`<td><span class="label">体重：</span><span field="">([\d]+)KG</span></td>`)
var genderRe = regexp.MustCompile(`<td><span class="label">性别：</span><span field="">([^<]+)</span></td>`)
var educationRe  = regexp.MustCompile(`<td><span class="label">学历：</span>([^<]+)</td>`)
var occupationRe = regexp.MustCompile(`<td><span class="label">职业：</span><span field="">([^<]+)</span></td>`)
var hokouRe = regexp.MustCompile(`<td><span class="label">籍贯：</span>([^<]+)</td>`)
var xinzuoRe = regexp.MustCompile(`<td><span class="label">星座：</span><span field="">([^<]+)</span></td>`)
var houseRe = regexp.MustCompile(`<td><span class="label">住房条件：</span><span field="">([^<]+)</span></td>`)
var carRe = regexp.MustCompile(`<td><span class="label">是否购车：</span><span field="">([^<]+)</span></td>`)
var idUrlRe = regexp.MustCompile(`http://album.zhenai.com/u/([\d]+)`)
func ParseProfile(contents []byte,url ,name string) engine.ParserResult{
	profile := model.Profile{}
	profile.Name = name
	// 年龄
	age, err := strconv.Atoi(extractString(contents, ageRe))
	if err != nil {
		panic(err)
	}
	if err == nil {
		profile.Age = age
	}
	// 婚况
	profile.Marriage = extractString(contents,marriageRe)
	// 身高
	height ,err :=strconv.Atoi(extractString(contents,heightRe))
	if err == nil {
		profile.Height = height
	}
	// 体重
	weight, err := strconv.Atoi(extractString(contents, weightRe))
	if err == nil {
		profile.Weight = weight
	}
	// 月收入
	profile.Income = extractString(contents,incomeRe)
	// 性别
	profile.Gender = extractString(contents,genderRe)
	// 学历
	profile.Education = extractString(contents,educationRe)
	// 职业
	profile.Occupation = extractString(contents,occupationRe)
	// 籍贯
	profile.Hokou = extractString(contents,hokouRe)
	// 星座
	profile.Xinzuo = extractString(contents,xinzuoRe)
	// 房子
	profile.House = extractString(contents,houseRe)
	// 车子
	profile.Car = extractString(contents,carRe)

	result := engine.ParserResult{
		Items: [] engine.Item{
			{
				Url:			url,
				Type:		"zhenai",
				Id:			extractString([]byte(url),idUrlRe),
				Payload:	profile,
			},
		},
	}

	matches := guessRe.FindAllSubmatch(contents,-1)
	for _, m := range matches {
		result.Requests = append(result.Requests,engine.Request{
			Url:				string(m[1]),
			ParserFunc:ProfileParser(string(m[2])),
		})
	}

	return result
}

func extractString(contents []byte,re *regexp.Regexp) string {
	subMatch := re.FindSubmatch(contents)
	if len(subMatch) >= 2 {
		return string(subMatch[1])
	}else {
		return ""
	}
}

func ProfileParser(name string) engine.ParserFunc{
	return func(c []byte, url string) engine.ParserResult {
		return ParseProfile(c,url,name)
	}
}